package com.elecbook.auth.service;

import com.alibaba.fastjson.JSON;

import com.elecbook.auth.model.dto.LoginDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserDetailsService {

    @Autowired
    ApplicationContext applicationContext;

    /**
     * description username即使loginDtoStr，就是实体loginDTO的字符串形式
     *
     * @author zken
     * CreateDate 2024/11/2 22:45:38
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LoginDTO loginDTO = JSON.parseObject(username, LoginDTO.class);
        String authType = loginDTO.getAuthType();

        String beanPrefix = "auth_";
        // 构建名称，根据名称获取bean
        AuthService authService = applicationContext.getBean(beanPrefix + authType, AuthService.class);
        return authService.login(loginDTO);
    }
}
